import {
	sub
} from '@/common/event.js'
import socket from '@/common/websocket.js'
import store from '@/store/index.js'
import { getRqst } from '../../common/request'
export default {
	namespaced: true,
	state: {
		status: "normal",
		videocallinfo:{
			
		},
		rooms:[]
	},
	mutations: {
		initRooms: (state, payload)=>{
			state.rooms = payload
		},
		setStatusState: (state, payload) => {
			state.status = payload
		},
		setVideocallinfoState: (state, payload) => {
			state.videocallinfo = payload
		},
	},
	actions: {
		async initRooms({
			commit
		}, {
			payload
		}) {
			console.log("vuex rooms action", payload)
			const ret = await getRqst().post(`common/common/getAllRooms`)
			console.log(33333333,ret.data)
			if(ret.data){
				commit(`initRooms`,ret.data)
			}
			
		},
	},
	init: async function() {
		console.log(`执行common初始化`)
		store.dispatch({
			type:"common/initRooms"
		})
		sub(`socket_createRoom`,function(){
			store.dispatch({
				type:"common/initRooms"
			})
		})
		sub(`socket_MT_someoneJoinRoom`,function(){
			store.dispatch({
				type:"common/initRooms"
			})
		})
		sub(`socket_MT_someoneLeaveRoom`,function(){
			store.dispatch({
				type:"common/initRooms"
			})
		})
		sub(`socket_videocallinvite`,function(e){
			if(store.state.common.status == "videocall"){
				socket.send({
					type:"iambusy",
					data:{
						friend_id:e.friend_id
					}
				})
			}else{
				store.commit(`common/setStatusState`,"videocall")
				store.commit(`common/setVideocallinfoState`,e)
				uni.navigateTo({
					url:"/pages/chat/friend/videocallrsv?id="+e.friend_id
				})
			}
		})
		
		sub(`socket_iambusy`,function(e){
			if(store.state.common.status == "videocall"){
				uni.showModal({
					title:"对方忙",
					complete() {
						store.commit(`common/setStatusState`,"normal")
						store.commit(`common/setVideocallinfoState`,{})
						uni.navigateBack()
					}
				})
			}
		})
		
		sub(`socket_cancelvideocall`,function(e){
			if(store.state.common.status == "videocall"){
				uni.showModal({
					title:"对方取消了视频聊天请求",
					complete() {
						store.commit(`common/setStatusState`,"normal")
						store.commit(`common/setVideocallinfoState`,{})
						uni.navigateBack()
					}
				})
			}
		})
		
		sub(`socket_refusevideocall`,function(e){
			if(store.state.common.status == "videocall"){
				uni.showModal({
					title:"对方拒绝了视频聊天请求",
					complete() {
						store.commit(`common/setStatusState`,"normal")
						store.commit(`common/setVideocallinfoState`,{})
						uni.navigateBack()
					}
				})
			}
		})
		
		sub(`socket_agreevideocall`,function(e){
			if(store.state.common.status == "videocall"){
				setTimeout(()=>{
					store.commit(`common/setStatusState`,"videomt")
					store.commit(`common/setVideocallinfoState`,e)
				},1000)
				uni.redirectTo({
					url:"/pages/chat/friend/videocallrtc?id="+e.friend_id+"&name="+e.friend_name
				})
			}
		})
		
		sub(`socket_hangoffvideocall`,function(e){
			console.log(333,e,store.state.common.status)
			if(store.state.common.status == "videomt" || store.state.common.status == "videocall"){
				uni.showToast({
					title:"视频通话结束",
					icon:"none"
				})
				store.commit(`common/setStatusState`,"normal")
				store.commit(`common/setVideocallinfoState`,{})
				uni.navigateBack()
			}
		})
		
	}
}